using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace SharpObjects.Utils
{
/*
			//#region ISerializable Members
			// example:

		public LogEntry(SerializationInfo info, StreamingContext context)
		{
			m_time = info.GetDateTime("m_time");
			m_sCategory = info.GetString("m_sCategory");
			m_sLocation = info.GetString("m_sLocation");
			m_sMachineName = info.GetString("m_sMachineName");
			m_sProcessName = info.GetString("m_sProcessName");
			m_nProcessId = info.GetInt32("m_nProcessId");
			m_nThreadId = info.GetInt32("m_nThreadId");
			m_sThreadName = info.GetString("m_sThreadName");
			m_sMessageId = info.GetString("m_sMessageId");
			m_sMessage = info.GetString("m_sMessage");
			m_sContext = info.GetString("m_sContext");
		}

		public void GetObjectData(SerializationInfo info, StreamingContext context)
		{
			info.AddValue("m_time", m_time);
			info.AddValue("m_sCategory", m_sCategory);
			info.AddValue("m_sLocation", m_sLocation);
			info.AddValue("m_sMachineName", m_sMachineName);
			info.AddValue("m_sProcessName", m_sProcessName);
			info.AddValue("m_nProcessId", m_nProcessId);
			info.AddValue("m_nThreadId", m_nThreadId);
			info.AddValue("m_sThreadName", m_sThreadName);
			info.AddValue("m_sMessageId", m_sMessageId);
			info.AddValue("m_sMessage", m_sMessage);
			info.AddValue("m_sContext", m_sContext);
		}


			//#endregion


*/

	public class FilePersistence
	{
		protected static BinaryFormatter m_formatter = new BinaryFormatter();

		public static void Save(object obj, string sFile)
		{
			using(FileStream stream = File.OpenWrite(sFile))
			{
				m_formatter.Serialize(stream, obj);
			}
		}

		public static void Save(object obj, Stream stream)
		{
			m_formatter.Serialize(stream, obj);
		}

		public static object Load(string sFile, object default_object)
		{
			object obj = null;
			
			if(File.Exists(sFile))
			{
				using(FileStream stream = File.OpenRead(sFile))
				{
					obj = m_formatter.Deserialize(stream);
					return obj;
				}
			}
			else
			{
				return default_object;
			}

		}

		public static object Load(Stream stream)
		{
			object obj = null;
			
			obj = m_formatter.Deserialize(stream);
			return obj;
		}

	}
}
